package com.shell.customer.accreditation.rx;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.keyword.EsignAuthKeyword;
import com.shell.customer.accreditation.service.ContBasSnService;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubProtocol;

@Component
public class ContApplySubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private ContService contService;
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private ContBasSnService contBasSnService;

	public void onApplicationEvent(AccreditEvent event) {
		AccreditationVo accreditationVo = event.getAccreditationVo();
		CusRec cus = accreditationVo.getCustomerC();
		String inviteCode = accreditationVo.getInviteCode();
		CusRec platform = getPlatform();
		List<ContBas> conts = this.contService.selectConts(cus.getCusId(), "20", null);
		ContBas cont = null;
		if ((conts == null) || (conts.size() == 0)) {
			cont = genCont(cus, platform, inviteCode);
		} else {
			cont = (ContBas) conts.get(0);
		}
		event.setValue("cont", cont);
	}

	private CusRec getPlatform() {
		CusRec platform = this.cusRecService.getPlatform(null);
		if (platform == null) {
			throw new UnCheckMsgException("����������������");
		}
		return platform;
	}

	private ContBas genCont(CusRec cus, CusRec platform, String inviteCode) {
		String contSn = this.contBasSnService.createContSn(cus.getCusName(), null, inviteCode);
		EsignAuthKeyword keyword = new EsignAuthKeyword();
		keyword.setEntName(cus.getCusName());
		keyword.setAddress(cus.getAddress());
		if ("PERSONAL".equals(cus.getCusType())) {
			keyword.setLegalNm("");
		} else {
			keyword.setLegalNm(cus.getLrName());
		}
		keyword.setContactPhone(cus.getContactorPhone());
		keyword.setPlatformNm(platform.getCusName());
		keyword.setPlatformAddress(platform.getAddress());
		keyword.setPlatformLegalNm(platform.getLrName());
		keyword.setPlatformContactPhone(platform.getContactorPhone());
		keyword.setAuthContSn(contSn);

		PubProtocol pubProtocol = this.pubProtocolFeign.getPubProtocolByTypeAndGrpId("authen_proto", "", "");
		List<ContTmpl> tmpls = this.contService.getContTmpl("20");
		String protTitle = "����";
		ContBas cont = new ContBas();
		String contId = SystemUtil.getUUID();
		cont.setContId(contId);

		cont.setContTitle(protTitle);
		cont.setBizType("20");
		cont.setBizId(cus.getCusId());
		cont.setSponsor(cus.getCusId());
		cont.setAcceptor(platform.getCusId());
		cont.setContDate(SysDateUtil.getSystemDate());
		cont.setCreateTime(SysDateUtil.getSystemDateTime());
		cont.setContSn(contSn);
		List<ContProt> prots = new ArrayList();
		if ((pubProtocol != null) && (tmpls != null) && (tmpls.size() > 0)) {
			String content = ContTemplateHelper.replaceContent(pubProtocol.getContent(), keyword);
			ContTmpl tmpl = (ContTmpl) tmpls.get(0);
			ContProt prot = this.contService.genProtWithContent(tmpl.getTmplId(), protTitle, content, true);
			prots.add(prot);
		}
		cont.setContProgress("0");
		this.contService.saveCont(cont, prots);
		return cont;
	}
}
